﻿using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Office.Interop.PowerPoint;
using Microsoft.Office.PowerPoint.STAMP.Properties;

namespace Microsoft.Office.PowerPoint.STAMP.Core
{
    /// <summary>
    /// Validates captions
    /// </summary>
    public class CaptionValidator
    {
        private List<string> _validationErrors = new List<string>();
        private readonly Shape _mediaShape;

        /// <summary>
        /// Initializes a new instance of the <see cref="CaptionValidator"/> class.
        /// </summary>
        /// <param name="mediaShape">The media shape.</param>
        public CaptionValidator(Shape mediaShape)
        {
            _mediaShape = mediaShape;
        }

        /// <summary>
        /// Gets the validation errors.
        /// </summary>
        public List<string> ValidationErrors
        {
            get { return _validationErrors; }
        }

        /// <summary>
        /// Determines if the captions are valid or not
        /// Creates a list of validation errors when appropriate
        /// </summary>
        /// <param name="captionItemsList">List of captions to validate.</param>
        /// <returns></returns>
        public bool Validate(CaptionItemsList captionItemsList)
        {
            ValidationErrors.Clear();

            var mediaEndPoint = _mediaShape.MediaFormat.EndPoint;
            ConcurrentBag<string> temporaryValidationErrors = new ConcurrentBag<string>();
            captionItemsList.CaptionItems.AsParallel().ForAll(captionItem => ValidateCaption(captionItem, mediaEndPoint, temporaryValidationErrors));
            _validationErrors = temporaryValidationErrors.ToList();
            return ValidationErrors.Count == 0;
        }

        private static void ValidateCaption(CaptionItem caption, int mediaEndPoint, ConcurrentBag<string> temporaryValidationErrors)
        {
            if (caption.BookMarkEntryPosition > mediaEndPoint)
            {
                string validationMessage = String.Format(Resources.CaptionManager_Validate_StartTime_MediaLength,
                                                         caption.BookMarkExitPosition.ToTimeSpanString(),
                                                         mediaEndPoint.ToTimeSpanString());
                temporaryValidationErrors.Add(validationMessage);
            }

            if (caption.BookMarkExitPosition > mediaEndPoint)
            {
                string validationMessage = String.Format(Resources.CaptionManager_Validate_EndTime_MediaLength,
                                                         caption.BookMarkExitPosition.ToTimeSpanString(),
                                                         mediaEndPoint.ToTimeSpanString());
                temporaryValidationErrors.Add(validationMessage);
            }

            if (caption.BookMarkEntryPosition > caption.BookMarkExitPosition)
            {

                string validationMessage = String.Format(Resources.CaptionManager_Validate_StartTime_Greater_EndTime,
                                                         caption.BookMarkEntryPosition.ToTimeSpanString(),
                                                         caption.BookMarkExitPosition.ToTimeSpanString());
                temporaryValidationErrors.Add(validationMessage);
            }

            if (String.IsNullOrEmpty(caption.Text))
            {
                string validationMessage = Resources.CaptionManager_Validate_Text_Empty;
                temporaryValidationErrors.Add(validationMessage);
            }
        }
    }
}